// Package requests 处理请求数据和表单验证
package requests

import (
	"github.com/gofiber/fiber/v2"
	"github.com/thedevsaddam/govalidator"
)

type SignupPhoneExistRequest struct {
	Phone string `json:"phone,omitempty" valid:"phone"`
}
type SignupEmailExistRequest struct {
	Email string `json:"email,omitempty" valid:"email"`
}

type MiniprogramLoginRequest struct {
	Code string `json:"code,omitempty" valid:"code"`
}

func ValidateMiniprogramLogin(c *fiber.Ctx, request *MiniprogramLoginRequest) map[string][]string {
	c.QueryParser(request)

	rules := govalidator.MapData{
		"code": []string{"required"},
	}

	messages := govalidator.MapData{
		"code": []string{
			"required:code 必传",
		},
	}

	return Validate(request, rules, messages)
}

func ValidateSignupPhoneExist(c *fiber.Ctx, request *SignupPhoneExistRequest) map[string][]string {

	c.BodyParser(request)

	rules := govalidator.MapData{
		"phone": []string{"required", "digits:11"},
	}

	// 自定义验证出错时的提示
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号为必填项，参数名称 phone",
			"digits:手机号长度必须为 11 位的数字",
		},
	}

	return Validate(request, rules, messages)
}
